const app = getApp()
import {
    addGodWorker
} from '../../api/app.js'
import {
    wxfileTobase64,
    inputchange
} from '../../utils/util.js'
import user from '../../utils/user.js'
Component({
    data: {
        cardId: '',
        name: '',
        companyName: '',
        fileList: [],
        remark: '',
    },
    properties: {
        godMsg: Object
    },
    observers: {},
    ready: function () {
        this.setData({
            companyName: user.companyName,
        })
        if (this.data.godMsg) {
            this.setData({
                cardId: this.data.godMsg.idCardNo,
                name: this.data.godMsg.workerName
            })
        }
    },
    methods: {
        inputchange(event) {
            inputchange(event, this)
        },
        // 上传图片
        upload() {
            const t = this
            wx.chooseImage({
                count: 4,
                sizeType: ['compressed'],
                sourceType: ['album', 'camera'],
                success(res) {
                    console.log(res)
                    // tempFilePath可以作为img标签的src属性显示图片
                    const tempFilePaths = t.data.fileList
                    if ((tempFilePaths.length + res.tempFilePaths.length) > 4) {
                        app.toast('最多只能上传4张图片！')
                        return 0
                    }
                    tempFilePaths.push(...res.tempFilePaths)
                    t.setData({
                        fileList: tempFilePaths
                    })
                }
            })
        },
        submit() {
            if (/[0-9]/.test(this.data.name)) {
                app.toast('姓名不可携带数字')
                return 0
            }
            if (!this.data.name) {
                app.toast('请输入姓名！')
                return 0
            }
            console.log(this.data)
            if (!/^\d{17}[\dXx]$/.test(this.data.cardId)) {
                app.toast('请输入正确的身份证！')
                return 0
            }
            const promiseList = []
            for (const i in this.data.fileList) {
                promiseList.push(wxfileTobase64(this.data.fileList[i]))
            }
            Promise.all(promiseList).then(result => {
                console.log(this)
                addGodWorker({
                    userName: this.data.name,
                    idCardNo: this.data.cardId,
                    companyId: user.companyId,
                    remark: this.data.remark,
                    createUserId: user.userId,
                    params: {
                        photos: result
                    }
                }).then(() => {
                    app.toast('提交成功！', 'success')
                    setTimeout(() => {
                        this.triggerEvent('complete')
                    }, 1000)
                })
            })
        },
        close() {
            this.triggerEvent('cancel')
        }
    }

})